<?
require "include/bittorrent.php";
dbconn(false);
loggedinorreturn();

if (get_user_class() < UC_MODERATOR)
error("Permission denied.");

$pollid = (int)$_GET["id"];
$selectedpollnum = get_row_count("polls", "WHERE $pollid >= id");
$jump = false;

stdhead("Poll Overview");

print("<h1>Polls</h1>\n");

print("<table width=750 border=1 cellspacing=0 cellpadding=5>\n<tr><td class=colhead align=center>ID</td><td class=colhead>Added</td><td class=colhead>Question</td></tr>\n");

query("SET @c=0");
$res = query("SELECT id, added, question, @c:=@c+1 as pollnum FROM polls ORDER BY id");
while ($arr = mysql_fetch_assoc($res))
{
	if ($arr['pollnum'] == 1 || $arr['pollnum'] == mysql_num_rows($res) || ($arr['pollnum'] >= $selectedpollnum - 2 && $arr['pollnum'] <= $selectedpollnum + 2))
	{
		$added = date("Y-m-d h-i-s",strtotime($arr['added'])) . " PST (" . (get_elapsed_time(sql_timestamp_to_unix_timestamp($arr['added']))) . " ago)";
		print("<tr");
		if ($arr['id'] == $pollid)
			print(" style=\"background-color: #BBBBBB\"");
		print("><td align=center><a href=polloverview.php?id=$arr[id]>$arr[id]</a></td><td>$added</td><td><a href=polloverview.php?id=$arr[id]>$arr[question]</a></td></tr>\n");
		$jump = true;
	}
	else if ($jump == true)
	{
		print("<tr><td align=center>...</td><td>...</td><td>...</td></tr>\n");
		$jump = false;
	}
}

print("</table>\n");

if ($pollid)
{
$res = query("SELECT * FROM polls WHERE id = $pollid LIMIT 1") or sqlerr();

if (mysql_num_rows($res) == 0) 
{
print("<p><br><b>There are no polls with that ID!</b></p>\n");
stdfoot();
exit;
}

$arr = mysql_fetch_assoc($res);

$o = array($arr["option0"], $arr["option1"], $arr["option2"], $arr["option3"], $arr["option4"],
$arr["option5"], $arr["option6"], $arr["option7"], $arr["option8"], $arr["option9"],
$arr["option10"], $arr["option11"], $arr["option12"], $arr["option13"], $arr["option14"],
$arr["option15"], $arr["option16"], $arr["option17"], $arr["option18"], $arr["option19"]);

print("<br>\n<h1>Poll Options</h1>\n");
print("<table width=750 border=1 cellspacing=0 cellpadding=5>\n<tr><td class=colhead>Option No</td><td class=colhead>Option</td><td class=colhead>Votes</td></tr>\n");
foreach($o as $key=>$value) 
{
$votes = number_format(get_row_count("pollanswers", "WHERE selection=$key AND pollid=$pollid"));
if($value != "")
print("<tr><td>$key</td><td>$value</td><td>$votes</td></tr>\n");
}
print("</table>\n");

$res = query("SELECT pollanswers. * , users.username FROM pollanswers LEFT JOIN users ON users.id = pollanswers.userid WHERE pollid = $pollid AND selection < 20 ORDER BY pollanswers.selection") or sqlerr();

print("<br>\n<h1>User Votes</h1>\n");

print("<table width=750 border=1 cellspacing=0 cellpadding=5>\n<tr><td class=colhead>User</td><td class=colhead>Selection</td></tr>\n");

if (mysql_num_rows($res) == 0) 
{
print("<tr><td colspan=2>No users have voted!</td></tr>\n</table>\n");
stdfoot();
exit;
}

while ($arr = mysql_fetch_assoc($res))
{
if ($arr['username'])
$user = "<a href=userdetails.php?id=$arr[userid]><b>$arr[username]</b></a>";
else
$user = "<b>[Deleted user]</b>";
if ($o[$arr['selection']])
$selection = $o[$arr['selection']];
else
$selection = "[Deleted option]";

print("<tr><td>$user</td><td>$selection</td></tr>\n");
}

print("</table>\n");

}

stdfoot();
?>